package com.michael.baseketball.ui

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.viewpager2.widget.ViewPager2
import com.michael.baseketball.R
import com.michael.baseketball.base.BaseActivity
import com.michael.baseketball.databinding.ActivityWelcomeBinding
import com.seuic.baseketball.util.CacheUtil
import com.zhpan.bannerview.BannerViewPager
import com.zhpan.bannerview.BaseBannerAdapter
import com.zhpan.bannerview.BaseViewHolder

import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel

import me.hgj.jetpackmvvm.demo.app.util.SettingUtil

import me.hgj.jetpackmvvm.ext.view.gone
import me.hgj.jetpackmvvm.ext.view.visible

/**
 * 作者　: hegaojian
 * 时间　: 2020/2/22
 * 描述　:
 */
@Suppress("DEPRECATED_IDENTITY_EQUALS")
class WelcomeActivity : BaseActivity<BaseViewModel, ActivityWelcomeBinding>() {

    private var resList = arrayOf("唱", "跳", "r a p")



    override fun initView(savedInstanceState: Bundle?) {




        //防止出现按Home键回到桌面时，再次点击重新进入该界面bug
        if (intent.flags and Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT !== 0) {
            finish()
            return
        }
        mDatabind.click = ProxyClick()
        mDatabind. welcomeBaseview.setBackgroundColor(SettingUtil.getColor(this))

        if (CacheUtil.isFirst()) {
            //是第一次打开App 显示引导页
            mDatabind.welcomeImage.gone()
            mDatabind.bannerView.apply {
//                adapter = object: BaseBannerAdapter<String, WelcomeBannerViewHolder>(){
//                    override fun createViewHolder(p0: View, p1: Int): WelcomeBannerViewHolder {
//                        return WelcomeBannerViewHolder(p0);
//                    }
//
//                    override fun onBind(
//                        p0: WelcomeBannerViewHolder?,
//                        p1: String?,
//                        p2: Int,
//                        p3: Int
//                    ) {
//                        p0?.bindData(p1, p2, p3);
//                    }
//
//                    override fun getLayoutId(p0: Int)=R.layout.banner_itemwelcome
//
//                }
                setLifecycleRegistry(lifecycle)
                registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                    override fun onPageSelected(position: Int) {
                        super.onPageSelected(position)
                        if (position == resList.size - 1) {
                            mDatabind.welcomeJoin.visible()
                        } else {
                            mDatabind.welcomeJoin.gone()
                        }
                    }
                })
                create(resList.toList())
            }
        } else {
            //不是第一次打开App 0.3秒后自动跳转到主页
            mDatabind.welcomeImage.visible()
            mDatabind.bannerView.postDelayed({
                startActivity(Intent(this, MainActivity::class.java))
                finish()
                //带点渐变动画
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
            }, 300)
        }
    }

    inner class ProxyClick {
        fun toMain() {
            CacheUtil.setFirst(false)
            startActivity(Intent(this@WelcomeActivity, MainActivity::class.java))
            finish()
            //带点渐变动画
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
        }
    }

    inner class WelcomeBannerViewHolder(view: View) : BaseViewHolder<String>(view) {
        override fun bindData(data: String?, position: Int, pageSize: Int) {
            val textView = findView<TextView>(R.id.banner_text)
            textView.text = data
        }

    }

    override fun layoutId()= R.layout.activity_welcome

}